<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of ModListasValores
 *
 * @author EADeveloper
 */
class ModListasValores implements EaSoftModule {

    public static $ALUMNOS_LOV = "lovalumnos";
    public static $BENEFACTORES_LOV = "lovalumnos";
    public static $CONTA_DEBE_LOV = "debelov";
    public static $CONTA_HABER_LOV = "haberlov";
    public static $LOV_PARAM = "LOV";
    public static $LOV_TERM = "term";
    public $db;
    public $instanceName="ModListasValores";
    public $includePath;

    //put your code here
    public function getCssFiles() {
        
    }

    public function getJsFiles() {
        
    }

    public function getWindowTitle() {
        
    }

    public function main() {
        switch (getParam(self::$LOV_PARAM)) {
            case self::$ALUMNOS_LOV: {
                    $p = getParam(self::$LOV_TERM);
                    $p = isEmpty($p) ? null : "where ID LIKE '$p%'";
                    $q = "select ID,CONCAT(FIRST_NAME,' ',LAST_NAME) AS NAME from cllg_student $p";
                    $this->getListJson($q, "ID", "NAME", $p);
                    break;
                }
            case self::$BENEFACTORES_LOV: {
                    $p = getParam(self::$LOV_TERM);
                    $p = isEmpty($p) ? null : "where ID LIKE '$p%'";
                    $q = "select ID, RAZON_SOCIAL from cllg_benefactor $p";
                    $this->getListJson($q, "ID", "RAZON_SOCIAL", $p);
                    break;
                }
           case self::$CONTA_DEBE_LOV: {
                    $p = getParam(self::$LOV_TERM);                    
                    $q = "select codigo,nombre from cllg_contab_cuenta where activo=1 and codigo like '$p%'";
                    $this->getListJson($q, "codigo", "nombre", $p);                    
                    break;
                }
           case self::$CONTA_HABER_LOV: {
                    $p = getParam(self::$LOV_TERM);                    
                    $q = "select codigo,nombre from cllg_contab_cuenta where pasivo=1 and codigo like '$p%'";
                    $this->getListJson($q, "codigo", "nombre", $p);
                    break;
                }     
        }
    }

    private function getListJson($query, $pkField, $descField) {
        $r = $this->db->queryToArray($query);
        $j = array();
        foreach ($r as $rws) {
            array_push($j, array("id" => $rws[$pkField], "value" => $rws[$pkField] . "-" . $rws[$descField]));
        }
        echo json_encode($j);
    }

}

?>
